﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Diagnostics.Contracts;

namespace KalenderTradisional
{
    public partial class UserControl2 : UserControl
    {
        //# Region
        private readonly string animals, gan, zhi;
        private TextBox txtLunar;
        private DateTimePicker lunarPicker;
        private ChineseLunisolarCalendar lunarCalendar;
//# Endregion

        public UserControl2()
        {
            InitializeComponent();
            gan = "B, C, D, E & F Gengxin Jen-kuei";
            zhi = "Zichou Mao Yin Chen Si Wu shen Xu Hai";
            animals = "Rat Ox Tiger Rabbit dragon snake horse sheep monkey Jigou pig";
            Application.CurrentCulture = CultureInfo.GetCultureInfo ("zh-CN"); // China region.
            lunarCalendar = new ChineseLunisolarCalendar (); // Chinese lunisolar calendar.
            txtLunar = new TextBox ();
            txtLunar.Dock = DockStyle.Bottom;
            txtLunar.ReadOnly = true; // read-only.
            this.Controls.Add (txtLunar);
            lunarPicker = new DateTimePicker ();
            lunarPicker.Dock = DockStyle.Top;
            //lunarPicker.Format = DateTimePickerFormat.Custom; // custom format.
            //lunarPicker.CustomFormat = "dddd yyyy 'years' M 'January' d 'day'";
            lunarPicker.ShowUpDown = true; // use the spin control.
            lunarPicker.MaxDate = lunarCalendar.MaxSupportedDateTime;
            lunarPicker.MinDate = lunarCalendar.MinSupportedDateTime;
            lunarPicker.ValueChanged += new EventHandler (lunarPicker_ValueChanged);
            lunarPicker.Value = DateTime.Today;
            this.Controls.Add (lunarPicker);
            this.AutoScaleMode = AutoScaleMode.Font; // font dimension control the zoom.
            this.Font = new Font (Font.Name, 24F);
            this.ClientSize = new Size (500, 88);
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink; // disable manual resizing.
            //this.SizeGripStyle = SizeGripStyle.Hide;
            //this.SizeGripStyle = SizeGripStyle.Hide; // hide the resize handles.
            //this.StartPosition = FormStartPosition.CenterScreen; // display in the center of the desktop.
            //# Endregion
            }

            //# Region calculation of the lunar calendar
            private void lunarPicker_ValueChanged (object sender, EventArgs e)
            {
            DateTime solar = lunarPicker.Value;
            int year = lunarCalendar.GetYear (solar);
            int month = lunarCalendar.GetMonth (solar);
            int day = lunarCalendar.GetDayOfMonth (solar);
            int leapMonth = lunarCalendar.GetLeapMonth (year);
            if (0 <leapMonth && leapMonth <= month)
            --month;
            
            //txtLunar.Text = String.Format (the lunar {0} {1} years {2} years {3} years {4} {5} day gan [(year - 4)% 10] zhi [(year - 4)% 12], animals [(year - 4)% 12], year, month, day); 
        }
    }
}
